package com.action.UnitInterval;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.entity.Device;
import com.entity.Parameter;
import com.entity.Substation;
import com.entity.UnitInterval;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.DeviceService;
import com.service.ParameterService;
import com.service.SubstationService;
import com.service.UnitIntervalService;

public class UnitIntervalAction extends ActionSupport implements SessionAware {

	private UnitIntervalService unitIntervalService;
	private DeviceService deviceService;
	private SubstationService substationService;
	private ParameterService parameterService;
	private String substationNo;
	private Map session;
	private String unitNo;
	private String[] deviceNo_select;
	private String result;
	private UnitInterval unitInterval;

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public void setSubstationService(SubstationService substationService) {
		this.substationService = substationService;
	}

	public void setDeviceService(DeviceService deviceService) {
		this.deviceService = deviceService;
	}

	public UnitIntervalService getUnitIntervalService() {
		return unitIntervalService;
	}

	public void setUnitIntervalService(UnitIntervalService unitIntervalService) {
		this.unitIntervalService = unitIntervalService;
	}

	public UnitInterval getUnitInterval() {
		return unitInterval;
	}

	public void setUnitInterval(UnitInterval unitInterval) {
		this.unitInterval = unitInterval;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String[] getDeviceNo_select() {
		return deviceNo_select;
	}

	public void setDeviceNo_select(String[] deviceNo_select) {
		this.deviceNo_select = deviceNo_select;
	}

	public String getUnitNo() {
		return unitNo;
	}

	public void setUnitNo(String unitNo) {
		this.unitNo = unitNo;
	}
	
	public String getSubstationNo() {
		return substationNo;
	}

	public void setSubstationNo(String substationNo) {
		this.substationNo = substationNo;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public String findUnitIntervalBySubstationNo() throws Exception {
		List<UnitInterval> UnitIntervallist = unitIntervalService
				.findUnitIntervalBySubstation(substationNo);
		Map session = ActionContext.getContext().getSession();
		session.put("unitIntervallist", UnitIntervallist);
		session.put("substationNo", substationNo);
		return SUCCESS;
	}

	public String findAuditingUnitInterval() throws Exception {
		List<UnitInterval> UnitIntervallist = new ArrayList<UnitInterval>();
		UnitIntervallist = unitIntervalService.findAuditingUnitInterval();
		session.put("unitIntervallist", UnitIntervallist);
		return SUCCESS;
	}
	
	public String initAddUnitInterval(){
		List<Parameter> Parameterlist=new ArrayList<Parameter>();
		Parameterlist = parameterService.findAllDeviceType();
		List<Substation> Substationlist=new ArrayList<Substation>();
		Substationlist = substationService.findAllSubstationByAvailable();
		List<Device> Devicelist=new ArrayList<Device>();
		Devicelist = deviceService.findDeviceNotInUnitInterval();
		session.put("deviceTypeList" , Parameterlist);
		session.put("substationList" , Substationlist);
		session.put("unitDeviceNoList" , Devicelist);
		session.remove("unitDeviceNoList");
		return SUCCESS;
	}
	
	public String changeAddUnitInterval(){
		List<Parameter> Parameterlist=new ArrayList<Parameter>();
		Parameterlist = parameterService.findAllDeviceType();
		List<Substation> Substationlist=new ArrayList<Substation>();
		Substationlist = substationService.findAllSubstation();
		List<Device> Devicelist=new ArrayList<Device>();
		Devicelist = deviceService.findDeviceBySubstationNotInUnitInterval(substationNo);
		session.put("deviceTypeList" , Parameterlist);
		session.put("substationList" , Substationlist);
		session.put("unitDeviceNoList" , Devicelist);
		session.put("substationNo", substationNo);
		return SUCCESS;
	}
	
	public String addUnitIntervalMethod(){
		String deviceNo = null;
		String deviceType = null;
		for(int i=0;i<deviceNo_select.length;i++){
			UnitInterval unitInterval = new UnitInterval();
			unitInterval.setUnitNo(unitNo);
			deviceNo = deviceNo_select[i];
			deviceType = deviceService.findDeviceByNo(deviceNo).getDeviceType();
			unitInterval.setDeviceNo(deviceNo);
			unitInterval.setDeviceType(deviceType);
			unitInterval.setSubstationNo(substationNo);
			unitInterval.setUnitStatus("1");
			unitIntervalService.addUnitInterval(unitInterval);
		}
		session.put("substationNo", "");
		return SUCCESS;
	}
	
	public String checkUnitIntervalNoAvailable(){
		if(unitIntervalService.checkUnitIntervalNoAvailable(unitNo))
			result = "true";
		else result = "false";
		return SUCCESS;
	}
	
	public String deleteUnitIntervalMethod(){
		unitIntervalService.deleteUnitIntervalByUnitNo(unitNo);
		return SUCCESS;
	}

}
